home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group94a.txt / 000115_icon-group-sender _Thu May 5 13:47:56 1994.msg < prev    next >
Internet Message Format  |  1994-08-19  |  10KB

  1. Received: by cheltenham.cs.arizona.edu; Thu, 5 May 1994 12:24:29 MST
  2. Date: Thu, 05 May 1994 13:47:56 -0600 (CST)
  3. From: Chris Tenaglia - 257-8765 <TENAGLIA@MIS.MCW.EDU>
  4. Subject: Bueno Cinco de Mayo
  5. To: icon-group@cs.arizona.edu
  6. Message-Id: <01HBZCVUT89U8WWD9C@mis.mcw.edu>
  7. Organization: Medical College of Wisconsin (Milwaukee, WI)
  8. X-Vms-To: IN%"icon-group@cs.arizona.edu"
  9. Mime-Version: 1.0
  10. Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
  11. Content-Transfer-Encoding: 7BIT
  12. Status: R
  13. Errors-To: icon-group-errors@cs.arizona.edu
  14.  
  15.  
  16. For the Cinco de Mayo I'd like to offer a little Icon code segment.
  17. This one was developed for unix (namely ultrix 4.3 rev 44). Maybe
  18. it will work on some other unix too. I'd like to know. This program
  19. is called 'when'. It's like a date based ls command. Some have told
  20. me 'find' can do the same things, but I find find a bit arcane?
  21.  
  22. So 'when' is what I use. Here are some samples :
  23.  
  24.      Csh% when before 4/12/92      files before a date
  25.      Csh% when before 300          files older than an age
  26.      Csh% when after 3/25          or younger than a date this year
  27.      Csh% when before 2/1/94 and after 10/31/93     even a range
  28.  
  29. More options and clauses are supported. Look at the code for clues.
  30. This one only works in the current directory. It also has an interesting
  31. property. Maybe this is just ultrix, maybe not, I'd like to know anyway...
  32. The interpreted version works fine, but the compiled version has a
  33. numeric overflow. That'll make for some fun debugging. I wrote it for
  34. myself as a tool to locate old files for archiving or deleting. Study and
  35. enjoy!
  36.  
  37. Chris Tenaglia (System Manager) |  "The past explained,     
  38. Medical College of Wisconsin    |   the future fortold, 
  39. 8701 W. Watertown Plank Rd.     |   the present largely appologized for."
  40. Milwaukee, WI 53226             |   Organon to The Doctor
  41. (414)257-8765                   |     
  42. tenaglia@mis.mcw.edu
  43.  
  44. #
  45. # file : when.icn
  46. # desc : date based ls command for unix
  47. # use  : iconx when [to|before __] [from|since __] [asc | des] [long|brief] [do __]
  48. #        brief output pipable to rm and others
  49. #        long will include day age, but not protection mask
  50. # spec : ls -al layout : month@33:3 day@37:2 yr@41:2|current
  51. #
  52. # update          by          what
  53. # 06-apr-1994     tenaglia    initial write
  54. #
  55. global base,      # 1970 calculation baseline number
  56.        today,     # displacement from 12:00:01am today
  57.        now,       # upto the second mark for right now
  58.        method,    # ascending or descending order
  59.        output,    # long (ls -al) or brief (ls -1) style
  60.        command,   # optional command to do on each file
  61.        files      # list with files, sizes, and ages
  62.  
  63. procedure main(param)
  64.   calc_today()
  65.   files   := directory()
  66.   method  := "none"
  67.   output  := "long"
  68.   command := ""
  69.   if *param = 0 then show_age()
  70.   every i := 1 to *param do
  71.     {
  72.     (option := param[i]) | break
  73.     case option of
  74.       {
  75.       "to"     |
  76.       "before" |
  77.       "until"   : {
  78.                   files := before(files,param[i+1])
  79.                   i    +:= 1
  80.                   }
  81.       "from"  |
  82.       "since" |
  83.       "after"   : {
  84.                   files := since(files,param[i+1])
  85.                   i    +:= 1
  86.                   }
  87.       "asc"     : method:="ascending"
  88.       "des"     : method:="descending"
  89.       "long"    : output:="long"
  90.       "brief"   : output:="brief"
  91.       "do"      : {
  92.                   every j := i+1 to *param do
  93.                     command ||:= param[j] || " "
  94.                   }
  95.       default   : 5    # stop("Unrecognized option :",option)
  96.       }
  97.     }
  98.   show_age()
  99.   end
  100.  
  101. #
  102. # just show another ls with days old numbers & optionally sorts
  103. #
  104. procedure show_age()
  105.   case method of
  106.     {
  107.     "none" : {  
  108.              every line := !files do
  109.                {
  110.                age := (today - parse(line,' ')[1]) / 86400
  111.                ks  := parse(line,' ')[2] / 1024
  112.                file:= line[23:0]
  113.                (command == "") |
  114.                  {
  115.                  write(command,line[37:0])
  116.                  system(command || line[37:0])
  117.                  next
  118.                  }
  119.                if output == "brief" then text := line[37:0]
  120.                                     else text:= right(age,6) || " days " || right(ks,6) || " kb | " || file
  121.                write(text)
  122.                }
  123.              }
  124.     "descending" : {
  125.                    results := sort(files)
  126.                    every line := !results do
  127.                      {
  128.                      age := (today - parse(line,' ')[1]) / 86400
  129.                      ks  := parse(line,' ')[2] / 1024
  130.                      file:= line[23:0]
  131.                      (command == "") |
  132.                        {
  133.                        write(command,line[37:0])
  134.                        system(command || line[37:0])
  135.                        next
  136.                        }
  137.                      if output == "brief" then text := line[37:0]
  138.                                           else text:= right(age,6) || " days " || right(ks,6) || " kb | " || file
  139.                      write(text)
  140.                      }
  141.                    }
  142.     "ascending" : {
  143.                    results := sort(files)
  144.                    every i := *results to 1 by -1 do
  145.                      {
  146.                      line:= results[i]
  147.                      age := (today - parse(line,' ')[1]) / 86400
  148.                      ks  := parse(line,' ')[2] / 1024
  149.                      file:= line[23:0]
  150.                      (command == "") |
  151.                        {
  152.                        write(command,line[37:0])
  153.                        system(command || line[37:0])
  154.                        next
  155.                        }
  156.                      if output == "brief" then text := line[37:0]
  157.                                           else text:= right(age,6) || " days " || right(ks,6) || " kb | " || file
  158.                      write(text)
  159.                      }
  160.                    }
  161.     default : 5
  162.     }
  163.   end
  164.  
  165. #
  166. # remove elements later than a date
  167. #
  168. procedure before(lst,days)
  169.   static  mtab
  170.   initial mtab := [0,31,59,90,120,151,181,212,243,273,304,334]
  171.   if find("/",days) then
  172.     {
  173.     mo := parse(days,'/')[1]
  174.     da := parse(days,'/')[2]
  175.     yr := parse(days,'/')[3] | parse(&date,'/')[1]
  176.     if yr < 100 then yr +:= 1900
  177.     tmp := yr * 31557600
  178.     dd  := mtab[mo] + da
  179.     if ((yr % 4) = 0) & (mo > 2) then dd +:= 1
  180.     tmp+:= dd * 86400
  181.     age := tmp
  182.     } else {
  183.     age := now - (days * 86400)
  184.     }
  185.   work := []
  186.   every file := !lst do
  187.     {
  188.     old := parse(file,' ')[1]
  189.     if old <= age then put(work,file)
  190.     }
  191.   return copy(work)
  192.   end
  193.  
  194. #
  195. # remove elements earlier than a date
  196. #
  197. procedure since(lst,days)
  198.   static  mtab
  199.   initial mtab := [0,31,59,90,120,151,181,212,243,273,304,334]
  200.   if find("/",days) then
  201.     {
  202.     mo := parse(days,'/')[1]
  203.     da := parse(days,'/')[2]
  204.     yr := parse(days,'/')[3] | parse(&date,'/')[1]
  205.     if yr < 100 then yr +:= 1900
  206.     tmp := yr * 31557600
  207.     dd  := mtab[mo] + da
  208.     if ((yr % 4) = 0) & (mo > 2) then dd +:= 1
  209.     tmp+:= dd * 86400
  210.     age := tmp
  211.     } else {
  212.     age := now - (days * 86400)
  213.     }
  214.   work := []
  215.   every file := !lst do
  216.     {
  217.     old := parse(file,' ')[1]
  218.     if old >= age then put(work,file)
  219.     }
  220.   return copy(work)
  221.   end
  222.  
  223. #
  224. # calculate today and now figures
  225. #
  226. procedure calc_today()
  227.   static  mtab
  228.   initial {
  229.           base := 1970*31557600
  230.           mtab := [0,31,59,90,120,151,181,212,243,273,304,334]
  231.           }
  232.   tmpy := parse(&date,'/')[1]
  233.   tmpm := parse(&date,'/')[2]
  234.   tmpd := parse(&date,'/')[3]
  235.   here := tmpy * 31557600 +
  236.           (mtab[tmpm] + tmpd) * 86400
  237.   if ((tmpy%4) = 0) & (tmpm > 2) then here +:= 86400
  238.   today := here
  239.   now   := here +
  240.            parse(&clock,':')[1] * 3600 +
  241.            parse(&clock,':')[2] *   60 +
  242.            parse(&clock,':')[3]
  243.   end
  244.  
  245. #
  246. # convert a ls -al output into a list for sorting and printing
  247. #
  248. procedure directory()
  249.   static mtab
  250.   initial {
  251.           mtab := table(0)
  252.           mtab["Jan"] := 0
  253.           mtab["Feb"] := 31
  254.           mtab["Mar"] := 59
  255.           mtab["Apr"] := 90
  256.           mtab["May"] := 120
  257.           mtab["Jun"] := 151
  258.           mtab["Jul"] := 181
  259.           mtab["Aug"] := 212
  260.           mtab["Sep"] := 243
  261.           mtab["Oct"] := 273
  262.           mtab["Nov"] := 304
  263.           mtab["Dec"] := 334
  264.           }
  265.   pipe    := open("ls -al","pr")
  266.   entries := []
  267.   every line := !pipe do
  268.     {
  269.     if any('dclst',line) then next   # ignore info and dirs
  270.     size := parse(line,' ')[4]
  271.     file := line[33:0]
  272.     day  := mtab[parse(line,' ')[5]] + parse(line,' ')[6]
  273.     year := if line[40] == " " then parse(line,' ')[7] else parse(&date,'/')[1]
  274.     sec  := if line[40] == " " then 0 else hhmm(parse(line,' ')[7])
  275.     mark := year * 31557600 + day * 86400 + sec
  276.     if (now-mark) < 0 then mark -:= 31557600
  277.     text := right(mark,12) || right(size,10) || " " || file
  278.     put(entries,text)
  279.     }
  280.   close(pipe)
  281.   return entries
  282.   end
  283.  
  284. #
  285. # convert hh:mm into seconds since midnight
  286. #
  287. procedure hhmm(str)
  288.   hh := str[1+:2]
  289.   mm := str[4+:2]
  290.   return hh*3600 + mm*60
  291.   end
  292.  
  293. #
  294. # parse a string into a list with respect to a delimiter
  295. #
  296. procedure parse(line,delims)
  297.   static chars
  298.   chars  := &cset -- delims
  299.   tokens := []
  300.   line ? while tab(upto(chars)) do put(tokens,tab(many(chars)))
  301.   return tokens
  302.   end
  303.  
  304.